<template>
  <el-card>
    <div class="search_container">
      <div class="search_form">
        <form action="/">
          <van-search v-model="modelvalue" show-action placeholder="请输入搜索关键词" @update:model-value="onupdate"
            @cancel="onCancel" @search="onSearch" />
        </form>
      </div>
      <div>
        <div class="hot_search" v-if="!modelvalue && historyList.length >0">
          <div style="display: flex; justify-content: space-between;">
            <div class="hot_title">历史记录</div>
            <el-button type="danger" :icon="Delete" color="rgba(102, 102, 102)"
              style="margin-top: 10px; margin-right: 10px;" @click="deleteData"/>
          </div>
          <nav class="list">
            <van-tag class="item" plain size="large" v-for="item, index in historyList" :key="index">{{ item
            }}</van-tag>
          </nav>
        </div>
        <div class="hot_search" v-if="!modelvalue">
          <div class="hot_title">热门搜索</div>
          <nav class="list">
            <van-tag class="item" plain size="large" v-for="item, index in hotSearchList.hotKeywordVOList"
              :key="item.keyword">{{ item.keyword }}</van-tag>
          </nav>
        </div>
      </div>
        <div class="tm-List" v-if="modelvalue">
          <div class="tm-listItem" v-for="item, index in updatemodelvalList" :key="index">
            <div class="inner">{{ item }}</div><i>></i>
          </div>
        </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Delete } from '@element-plus/icons-vue'
import { HotSearchModel, getHotSearch, getclickHotSearch } from '@/api/search';
import { onMounted } from 'vue';
const modelvalue = ref('');
const hotSearchList = ref<HotSearchModel>({
  defaultKeyword: [],
  defaultKeywords: [],
  hotKeywordVOList: [],
  colorfulEggList: []
})
const updatemodelvalList = ref<string[]>([])
const historyList = ref<string[]>(JSON.parse(localStorage.getItem('record') as string))
const onupdate = (value: string) => {
  // console.log(value)
  updatemodelvalue(value)
}
// JSON.parse(localStorage.getItem('record') as string)
// const a = (localStorage.setItem('record', []))
const onSearch = (val: string) => {
  let repetit = historyList.value.find(item => item == val)
  if (!repetit) {
    historyList.value.push(val)
    localStorage.setItem('record',JSON.stringify(historyList.value))
    modelvalue.value = ''
  } else {
    modelvalue.value = ''
  }
}
function deleteData (){
  historyList.value = []
}
onMounted(() => {
  getHotSearchHandler()
})
const onCancel = () => ('取消');
const getHotSearchHandler = async () => {
  try {
    let res = await getHotSearch()
    hotSearchList.value = res.data as HotSearchModel
  } catch (error) {
    console.error(error)
  }
}
const updatemodelvalue = async (value: string) => {
  try {
    let res = await getclickHotSearch(value)
    updatemodelvalList.value = res.data as string[]
  } catch (error) {
    console.error(error)
  }
}
</script>

<style lang="less" scoped>
.search_container {
  width: 100%;

  .hot_search {
    width: 100%;
    height: 160px;
    margin: 10px 15px 0;
    border-top: 1px solid rgba(238, 238, 238);

    .hot_title {
      width: 100%;
      height: 50px;
      font-size: 16px;
      line-height: 50px;
    }

    .list {
      width: 80%;
      padding: 0 20px;

      .tm-listItem {
        width: 100%;
        height: 58px;
        border: 1px solid #eee;
        display: flex;
        justify-content: space-between;
      }

      .item {
        margin-right: 12px;
      }
    }
  }

  .tm-List {
    width: 100%;

    .tm-listItem {
      width: 100%;
      height: 58px;
      display: flex;
      justify-content: space-between;
      font-size: 16px;
      line-height: 58px;
      border-bottom: 1px solid rgba(238, 238, 238);
    }
  }
}
</style>